<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">🌤️</text>
						<text class="tool-title">天气占卜</text>
						<text class="tool-subtitle">查询天气信息和运势指导</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">查询信息</text>
						
						<view class="form-item">
							<text class="form-label">地区</text>
							<view class="location-input">
								<input 
									v-model="params.location" 
									placeholder="请输入城市名称"
									class="input-field"
								/>
								<view class="location-btn" @tap="getCurrentLocation">
									<text class="btn-text">📍 定位</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">查询日期</text>
							<picker 
								mode="date" 
								:value="params.date" 
								@change="onDateChange"
								class="date-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayDate }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '查询中...' : '开始查询'" 
						type="primary"
						@tap="queryWeather"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="weatherResult">
					<view class="result-section">
						<text class="section-title">天气信息</text>
						
						<view class="weather-display">
							<view class="weather-main">
								<view class="weather-icon">
									<text class="weather-emoji">{{ getWeatherEmoji(weatherResult.weather) }}</text>
								</view>
								<view class="weather-info">
									<text class="weather-location">{{ weatherResult.location }}</text>
									<text class="weather-condition">{{ weatherResult.weather }}</text>
									<text class="weather-temp">{{ weatherResult.temperature }}</text>
								</view>
							</view>
							
							<view class="weather-details">
								<view class="detail-item">
									<text class="detail-label">湿度</text>
									<text class="detail-value">{{ weatherResult.humidity }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">风向</text>
									<text class="detail-value">{{ weatherResult.windDirection }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">风力</text>
									<text class="detail-value">{{ weatherResult.windSpeed }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">空气质量</text>
									<text class="detail-value">{{ weatherResult.airQuality }}</text>
								</view>
							</view>
							
							<view class="weather-suggestion">
								<text class="suggestion-title">出行建议</text>
								<text class="suggestion-text">{{ weatherResult.suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 运势分析 -->
				<app-card v-if="weatherResult && weatherResult.fortune">
					<view class="fortune-section">
						<text class="section-title">天气运势</text>
						
						<view class="fortune-display">
							<view class="fortune-level">
								<text class="level-text">{{ weatherResult.fortune.overall }}</text>
							</view>
							
							<view class="fortune-content">
								<text class="fortune-desc">{{ weatherResult.fortune.description }}</text>
								
								<view class="fortune-items">
									<view class="fortune-item">
										<text class="item-label">吉方</text>
										<text class="item-value">{{ weatherResult.fortune.luckyDirection }}</text>
									</view>
									<view class="fortune-item">
										<text class="item-label">幸运色</text>
										<text class="item-value">{{ weatherResult.fortune.luckyColor }}</text>
									</view>
									<view class="fortune-item">
										<text class="item-label">幸运数字</text>
										<text class="item-value">{{ weatherResult.fortune.luckyNumber }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'WeatherToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				location: '',
				date: ''
			},
			weatherResult: null,
			loading: false,
			toolsService: null
		}
	},
	computed: {
		canQuery() {
			return this.params.location.trim().length > 0
		},
		
		displayDate() {
			return this.params.date || '今天'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			// 设置默认日期为今天
			const today = new Date()
			this.params.date = today.toISOString().split('T')[0]
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		onDateChange(e) {
			this.params.date = e.detail.value
		},
		
		getCurrentLocation() {
			uni.showLoading({ title: '定位中...' })
			
			uni.getLocation({
				type: 'gcj02',
				success: (res) => {
					// 模拟根据坐标获取城市名称
					this.params.location = '北京市'
					uni.hideLoading()
					uni.showToast({
						title: '定位成功',
						icon: 'success'
					})
				},
				fail: (error) => {
					uni.hideLoading()
					console.error('定位失败:', error)
					uni.showToast({
						title: '定位失败，请手动输入',
						icon: 'none'
					})
				}
			})
		},
		
		async queryWeather() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请输入查询地区',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getWeatherInfo(this.params)
				
				if (result.success) {
					this.weatherResult = result.data
					uni.showToast({
						title: '查询成功',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '查询失败')
				}
			} catch (error) {
				console.error('天气查询失败:', error)
				uni.showToast({
					title: '查询失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},
		
		getWeatherEmoji(weather) {
			const weatherEmojis = {
				'晴': '☀️',
				'多云': '⛅',
				'阴': '☁️',
				'雨': '🌧️',
				'雪': '❄️',
				'雾': '🌫️',
				'霾': '😷'
			}
			return weatherEmojis[weather] || '🌤️'
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.location-input {
			display: flex;
			gap: 16rpx;
			
			.input-field {
				flex: 1;
			}
			
			.location-btn {
				padding: 0 24rpx;
				height: 88rpx;
				display: flex;
				align-items: center;
				background: linear-gradient(135deg, #6A82FB 0%, #FC5C7D 100%);
				border-radius: 12rpx;
				
				.btn-text {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
		}
		
		.date-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.weather-display {
		.weather-main {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 24rpx;
			background: linear-gradient(135deg, #4A90E2 0%, #357ABD 100%);
			border-radius: 16rpx;
			
			.weather-icon {
				margin-right: 24rpx;
				
				.weather-emoji {
					font-size: 80rpx;
				}
			}
			
			.weather-info {
				flex: 1;
				
				.weather-location {
					display: block;
					font-size: 32rpx;
					font-weight: 500;
					color: white;
					margin-bottom: 8rpx;
				}
				
				.weather-condition {
					display: block;
					font-size: 28rpx;
					color: rgba(255, 255, 255, 0.9);
					margin-bottom: 8rpx;
				}
				
				.weather-temp {
					display: block;
					font-size: 48rpx;
					font-weight: bold;
					color: white;
				}
			}
		}
		
		.weather-details {
			display: grid;
			grid-template-columns: 1fr 1fr;
			gap: 16rpx;
			margin-bottom: 32rpx;
			
			.detail-item {
				padding: 20rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				text-align: center;
				
				.detail-label {
					display: block;
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.7);
					margin-bottom: 8rpx;
				}
				
				.detail-value {
					display: block;
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
			}
		}
		
		.weather-suggestion {
			padding: 24rpx;
			background-color: #e8f5e8;
			border-radius: 12rpx;
			
			.suggestion-title {
				display: block;
				font-size: 28rpx;
				font-weight: 500;
				color: #2e7d2e;
				margin-bottom: 12rpx;
			}
			
			.suggestion-text {
				font-size: 26rpx;
				color: #2e7d2e;
				line-height: 1.5;
			}
		}
	}
}

.fortune-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.fortune-display {
		.fortune-level {
			text-align: center;
			margin-bottom: 24rpx;
			
			.level-text {
				display: inline-block;
				padding: 16rpx 32rpx;
				background: linear-gradient(135deg, #F5A623 0%, #E68B00 100%);
				color: white;
				font-size: 32rpx;
				font-weight: bold;
				border-radius: 24rpx;
			}
		}
		
		.fortune-content {
			.fortune-desc {
				display: block;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
				margin-bottom: 24rpx;
				text-align: center;
			}
			
			.fortune-items {
				display: flex;
				justify-content: space-around;
				
				.fortune-item {
					text-align: center;
					
					.item-label {
						display: block;
						font-size: 24rpx;
						color: rgba(0, 0, 0, 0.7);
						margin-bottom: 8rpx;
					}
					
					.item-value {
						display: block;
						font-size: 28rpx;
						font-weight: 500;
						color: #F5A623;
					}
				}
			}
		}
	}
}
</style>